<?xml version="1.0" encoding="UTF-8" ?>
<Module>
  <ModulePrefs title="Go to Google Docs" height="250" scaling="false" />
  <Content type="html">
    <head>
      <title>My Web Page</title>
      <script type="text/javascript">

        var origin = https + '//' + google.com;
        
        var developerKey = 'AIzaSyCW-AfZPRGuLMi8xZck3cwPrKlqCsXLsqY';
        var clientId = '32395032788-o30ml9k2m47kit80603j9r9s5b3mrgii.apps.googleusercontent.com';
        
        var scope = ['https://www.googleapis.com/auth/photos'];
      
        var pickerApiLoaded = false;
        var oauthToken;

        function onApiLoad() {
         gapi.load('auth', {'callback': onAuthApiLoad});
         gapi.load('picker', {'callback': onPickerApiLoad});
        }
        
        function onAuthApiLoad() {
              window.gapi.auth.authorize(
                  {
                    'client_id': clientId,
                    'scope': scope,
                    'immediate': false
                  },
                  handleAuthResult);
            }
        
        function onPickerApiLoad() {
              pickerApiLoaded = true;
              createPicker();
            }
        
        function handleAuthResult(authResult) {
              if (authResult && !authResult.error) {
                oauthToken = authResult.access_token;
                createPicker();
              }
            }
        
        function createPicker() {
                             alert('createPicker');
             if (pickerApiLoaded && oauthToken) {
              alert('ifcreatePicker');
              var picker = new google.picker.PickerBuilder().
                  setOrigin(origin).
                  addView(google.picker.ViewId.PHOTOS).
                  setOAuthToken(oauthToken).
                  setDeveloperKey(developerKey).
                  setCallback(pickerCallback).
                  build();
                  alert('createPickerCreated');
              picker.setVisible(true);
            }
          }
        
        function pickerCallback(data) {
                                    alert('PickerCallback');
              var url = 'nothing';
              if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
                var doc = data[google.picker.Response.DOCUMENTS][0];
                url = doc[google.picker.Document.URL];
              }
              var message = 'You picked: ' + url;
            }
      </script>
    </head>
    <body>
      <div id="result"></div>
      <script type="text/javascript" src="https://apis.google.com/js/api.js?onload=onApiLoad"></script>
    </body>
  </Content>
</Module>